/**
 * <pre>
 * Title: 		ClientRequestFacadeImpl.java
 * Project: 	SmsPortal
 * Author:		Richard
 * Create:	 	2013-7-5 下午01:34:29
 * Copyright: 	Copyright (c) 2013
 * Company:		Shenzhen Helper
 * <pre>
 */
package com.hengpeng.msls.service.client.impl;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.hengpeng.common.util.AnnotationScanHelper;
import com.hengpeng.msls.domain.TransMethod;
import com.hengpeng.msls.port.RemoteService;
import com.hengpeng.msls.port.RemoteTransaction;
import com.hengpeng.msls.port.client.message.TransMessage;
import com.hengpeng.msls.port.client.vo.client.WapTransType;
import com.hengpeng.msls.service.client.ClientRequestFacade;

/**
 * <pre>
 * 客户端请求服务接口实现
 * </pre>
 * @author Richard
 * @version 1.0, 2013-7-5
 */
@Service
public class ClientRequestFacadeImpl implements ClientRequestFacade, ApplicationContextAware
{
	private final static Log logger = LogFactory.getLog(ClientRequestFacadeImpl.class);

	private static final Map<WapTransType, TransMethod> transMethods = new HashMap<WapTransType, TransMethod>();

	@SuppressWarnings("unchecked")
	@Override
	public <T> T process(TransMessage jsonMessage, WapTransType transType, Class<T> returnType) throws Exception
	{
		TransMethod transMethod = transMethods.get(transType);
		Object managedBean = transMethod.getManagedBean();
		Method method = transMethod.getMethod();
		return (T) method.invoke(managedBean, jsonMessage);
	}

	@Override
	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		if (transMethods.size() == 0)
		{
			List<Class<?>> findAnnotationByType = AnnotationScanHelper.findAnnotationByType(RemoteService.class);
			for (Class<?> clazz : findAnnotationByType)
			{
				Service annotation = clazz.getAnnotation(Service.class);
				logger.info("加载服务模块：" + clazz.getName());
				String beanName = annotation.value();
				if (beanName.isEmpty())
				{
					String simpleName = clazz.getSimpleName();
					boolean isUpperCase = false;
					if (simpleName.length() > 1)
					{
						isUpperCase = Character.isUpperCase(simpleName.charAt(1));
					}
					if (isUpperCase)
					{
						beanName = simpleName;
					}
					else
					{
						beanName = simpleName.substring(0, 1).toLowerCase() + simpleName.substring(1);
					}
				}
				Object bean = context.getBean(beanName);
				Method[] declaredMethods = clazz.getDeclaredMethods();
				for (Method method : declaredMethods)
				{
					RemoteTransaction annotation2 = method.getAnnotation(RemoteTransaction.class);
					if (annotation2 != null)
					{
						WapTransType transType = annotation2.transType();
						logger.info("注册交易服务接口：" + transType + " - "
								+ (annotation2.value().isEmpty() ? method.getName() : annotation2.value()));
						transMethods.put(transType, new TransMethod(bean, method));
					}
				}
			}
		}
	}

}
